package com.example.fengsam.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;
import java.util.Objects;
import java.util.Random;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;


public class MessageActivity extends Activity {

    public final String ACTIVITY_TAG="msg";

    EventHandler eventHandler;

    Button mBtnBindPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        mBtnBindPhone=(Button)this.findViewById(R.id.btn_bind_phone);

        mBtnBindPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //注册手机号
                RegisterPage registerPage = new RegisterPage();
                //注册回调事件
                registerPage.setRegisterCallback(new EventHandler() {
                    @Override
                    //重写方法
                    //事件完成后调用
                    public void afterEvent(int event, int result, Object data) {
                        //判断结果是否已经完成
                        if (result == SMSSDK.RESULT_COMPLETE) {
                            //获取数据data
                            HashMap<String, Objects> maps = (HashMap<String, Objects>) data;
                            //国家
                            String country = String.valueOf(maps.get("country"));
                            //手机号
                            String phone = String.valueOf(maps.get("phone"));
                            submitUserInfo(country, phone);
                        }

                    }
                });
                //显示注册界面
                registerPage.show(MessageActivity.this);
            }
        });
//                //注册回调
//                SMSSDK.registerEventHandler(eventHandler);
//
//                eventHandler = new EventHandler() {
//                    @Override
//                    public void afterEvent(int event, int result, Object data) {
//                        super.afterEvent(event, result, data);
//                        if (result == SMSSDK.RESULT_COMPLETE) {
//                            //回调完成
//                            if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//                                //提交验证码成功
//                                @SuppressWarnings("unchecked") HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
//                                String country = (String) phoneMap.get("country");
//                                String phone = (String) phoneMap.get("phone");
//                                Log.d(ACTIVITY_TAG, "提交验证码成功--country=" + country + "--phone" + phone);
//                            } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//                                //获取验证码成功
//                                Log.d(ACTIVITY_TAG, "获取验证码成功");
//                            } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
//                                //返回支持发送验证码的国家列表
//                            }
//                        } else {
//                            ((Throwable) data).printStackTrace();
//                        }
//                    }
//                };
//            }
//        });

        // 如果希望在读取通信录的时候提示用户，可以添加下面的代码，并且必须在其他代码调用之前，否则不起作用；如果没这个需求，可以不加这行代码
//        SMSSDK.setAskPermisionOnReadContact(boolShowInDialog);

        // 创建EventHandler对象
//        eventHandler = new EventHandler() {
//            public void afterEvent(int event, int result, Object data) {
//                if (data instanceof Throwable) {
//                    Throwable throwable = (Throwable)data;
//                    String msg = throwable.getMessage();
//                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
//                } else {
//                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//                        // 处理你自己的逻辑
//                    }
//                }
//            }
//        };
//
//        // 注册监听器
//        SMSSDK.registerEventHandler(eventHandler);
            }


    public void submitUserInfo(String country,String phone){
        Random r=new Random();

        String uid=Math.abs(r.nextInt())+"";
        String nickname="fengsam";

        SMSSDK.submitUserInfo(uid,nickname,null,country,phone);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_message, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }
}
